package com.cyandpos.pad.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.modifypassword.presenter.ModifyPasswordPresenter;
import com.cyandpos.pad.modifypassword.presenter.ModifyPasswordPresenterImpl;
import com.cyandpos.pad.modifypassword.view.ModifyPasswordView;
import com.cyandpos.pad.presentation.AdPresentationUtil;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;

public class ModifyPasswordActivity extends Activity implements ModifyPasswordView,View.OnClickListener {
    private ModifyPasswordPresenter mModifyPasswordPresenter;
    private ProgressDialog dialog;
    private EditText yuanmima, xinmima, xinmima1;
    private Button ok;
    private Button ib_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFinishOnTouchOutside(false);// 让界面之外的其他地方无效
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 没有标题
        setContentView(R.layout.activity_changepassword);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        APApplication.getInstance().addActivity(this);
        new AdPresentationUtil().initDisplayData(this);
        mModifyPasswordPresenter = new ModifyPasswordPresenterImpl(this);
        dialog = new ProgressDialog(ModifyPasswordActivity.this);
        dialog.setCancelable(false);
        dialog.setMessage("正在修改密码，请稍等 ....");

        yuanmima = (EditText) findViewById(R.id.yuanmima);
        xinmima = (EditText) findViewById(R.id.newmima);
        xinmima1 = (EditText) findViewById(R.id.xinmima2);
        ib_back= (Button) findViewById(R.id.btn_cancel);
        ok = (Button) findViewById(R.id.btn_ok1);
        ok.setOnClickListener(this);
        ib_back.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_ok1:{
                String oldpwd = CommonUtils.getContent(yuanmima);
                String newpwd = CommonUtils.getContent(xinmima);
                String newpwd1 = CommonUtils.getContent(xinmima1);

                if (!newpwd1.equals(newpwd)) {
                    ToastUtils.showMsg("新密码不匹配");
                    xinmima.setText("");
                    xinmima1.setText("");
                    return;
                }
                mModifyPasswordPresenter.ModifyPassword(newpwd,oldpwd, ErpCommonData.loadConfig.getCashierId()+"");
            }
            break;
            case R.id.btn_cancel:
                finish();
                break;
            default:
                break;
        }
    }
    // 监听软键盘的enter事件

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == event.KEYCODE_ENTER
                && KeyEvent.ACTION_UP == event.getAction()) {
            /* 隐藏软键盘 */
            View view = getCurrentFocus();
            if (view.getId() ==yuanmima .getId()) {
                xinmima.setFocusable(true);
                xinmima.setFocusableInTouchMode(true);
                xinmima.requestFocus();
                CommonUtils.showSoftInput(xinmima);
                return true;
            } else if (view.getId() == xinmima.getId()) {
                xinmima1.setFocusable(true);
                xinmima1.setFocusableInTouchMode(true);
                xinmima1.requestFocus();
                CommonUtils.showSoftInput(xinmima1);
                return true;

            }else if(view.getId() == xinmima1.getId()){
                CommonUtils.hideSoftInput(xinmima1);
            } else {

                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MENU) {// MENU键
            // 监控/拦截菜单键
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }





    /**
     * 修改密码成功
     * @param newPassword
     */
    @Override
    public void onModifyPasswordSuccess(String newPassword) {
        ToastUtils.showMsg("修改密码成功");
        startActivity(new Intent(this, LoginActivity.class));
    }

    /**
     * 修改密码失败
     * @param errorMsg
     */

    @Override
    public void showLoadFailMsg(String errorMsg) {
        ToastUtils.showMsg("修改密码失败");
        yuanmima.setText("");
        xinmima.setText("");
        finish();
    }

    @Override
    public void showProgress() {
        dialog.show();

    }

    @Override
    public void hideProgress() {
        dialog.dismiss();

    }
}
